<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <title>制造物联中间件系统</title>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/xadmin.css">
    <script src="${pageContext.request.contextPath}/resources/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/xadmin.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/resources/myjs/system/system.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">

                        <div class="layui-inline layui-show-xs-block">
                            <select name="type" lay-verify="required">
                                <option value="res_group_type">资源组</option>
                                <option value="res_name">资源名称</option>
                                <option value="res_link">资源连接</option>
                            </select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="searchText" placeholder="请输入查询内容" autocomplete="off"
                                   class="layui-input">
                        </div>

                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="sreach"><i
                                    class="layui-icon">&#xe615;</i>
                            </button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-header">
                    <button class="layui-btn" id="update">更新</button>
                    点击更新即可刷新对应的资源
                </div>
                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table layui-form" lay-data="{id:'test'}" id="test" lay-filter="test"></table>
                </div>

            </div>
        </div>
    </div>
</div>
</body>
<%--###################################模板开始###################################--%>
<%--资源组--%>
<script type="text/html" id="resGroup">
    {{d.sysResGroup.resGroupType}}
</script>
<%--自定义复选框--%>
<script type="text/html" id="resCheckbox">
    <input type="checkbox" name="resCheckBox" title="" lay-skin="primary" value="{{d.id}}"
           data-id="{{ d.id }}">
</script>
<%--###################################模板结束###################################--%>
<script>
    layui.use(['form', 'table', 'jquery'], function () {
        var form = layui.form;
        var table = layui.table;
        var $ = layui.jquery;

        //定义一个全局变量存储选中的id,set可以去重
        var ids = new Set()
        var checkedNum = 0;
        var checkAllFlag = false;
        var allNum = 0//页面呈现数量的总数


        //监听更新按钮,获取当前选中的资源id
        $("#update").on("click", function () {
            //获取所有选中的元素
            let idList = Array.from(ids).join(",");
            $.get("${pageContext.request.contextPath}/system/roleRes/updateRoleRes", {
                resList: idList
            }, function (res) {
                layer.alert(res.msg);
            })
        })

        //实时监听input输入框,更新table
        $("input[name=searchText]").on("input", function () {
            //  具体代码
            let data = $("input[name=searchText]").val();
            if (data == '') {
                loadTable('${pageContext.request.contextPath}/system/res/list')
            }
        });

        //加载页面数据
        loadTable('${pageContext.request.contextPath}/system/res/list')


        //查询监听
        form.on('submit(sreach)',
            function (data) {
                loadTable('${pageContext.request.contextPath}/system/res/search?type=' + data.field.type + '&searchText=' + data.field.searchText)
                return false;
            });
        // 监听全选
        form.on('checkbox(checkall)', function (data) {
            if (data.elem.checked) {
                $("tbody input[name=resCheckBox]").prop('checked', true);
                $("tbody input[name=resCheckBox]").next().addClass("layui-form-checked")
                //将本页所有的id都添加到set中
                $("tbody input[name=resCheckBox]").each(function () {
                    ids.add($(this).val())
                })
                checkAllFlag = true
                checkedNum = allNum
            } else {
                $("tbody input[name=resCheckBox]").prop('checked', false);
                $("tbody input[name=resCheckBox]").next().removeClass("layui-form-checked")
                //将本页所有的id从set中去除
                $("tbody input[name=resCheckBox]").each(function () {
                    ids.delete($(this).val())
                })
                checkAllFlag = false
                checkedNum = 0
            }

            // form.render('checkbox');
        });

        function loadTable(url) {
            table.render({
                elem: '#test'
                , url: url //数据接口
                // 对我们请求过来的数据进行一次预处理
                , parseData(res) {
                    if (res.success) {
                        return {
                            "code": 0, //解析接口状态, 等于0代表成功
                            "msg": res.msg, //解析提示文本
                            "count": res.data.total, //解析数据长度
                            "data": res.data.list//解析数据列表
                        }
                    } else {
                        layer.msg(res.msg);
                        return {
                            "code": res.code, //解析接口状态, 等于0代表成功
                            "msg": res.msg, //解析提示文本
                            "count": res.data.total, //解析数据长度
                            "data": res.data.list//解析数据列表
                        }
                    }
                },
                request: {
                    //页码的参数名称，默认：page，指定了页码的key
                    pageName: 'pageNum',
                    // 指定了每页容量的key
                    limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                page: true, //开启分页
                cols: [
                    [ //表头
                        {
                            templet: "#resCheckbox",
                            title: "<input type='checkbox' name='resCheckBox' lay-skin='primary' lay-filter='checkall'> "
                        },
                        {field: 'resName', title: '资源名称'},
                        {field: 'resLink', title: '资源连接'},
                        {field: 'resType', title: '资源类型'},
                        {title: '资源组', templet: '#resGroup'}
                    ]
                ],
                done: function (tableData) {
                    checkedNum = 0;//每次加载新数据计数器清0
                    allNum = tableData.data.length
                    $.get("${pageContext.request.contextPath}/system/roleRes/findRoleRes", function (res) {
                        if (res.success) {
                            for (let i = 0; i < res.data.length; i++) {
                                $("tbody [name=resCheckBox][value='" + res.data[i] + "']").next().click()
                                ids.add(res.data[i].toString())
                            }
                        } else {
                            layer.alert(res.msg, {
                                icon: 6
                            });
                        }
                    })

                    $("tbody [name=resCheckBox]").next().on("click", function () {
                        //判断当前选中状态
                        if ($(this).prev().prop("checked")) {
                            //将id加入到ids
                            ids.add($(this).prev().val())
                            checkedNum++;
                        } else {
                            //将id从ids中去除
                            ids.delete($(this).prev().val())
                            checkedNum--;
                        }
                        checkAllHighlight(checkedNum, allNum)
                    })

                }
            });
        }

        //检验是否让全选框亮起
        function checkAllHighlight(currChecked, all) {
            if (currChecked == all) {
                $("thead input[name=resCheckBox]").prop('checked', true);
                $("thead input[name=resCheckBox]").next().addClass("layui-form-checked")
                checkAllFlag = true;
            } else {
                //判断是否已经亮起,如果已经亮起那么就让其熄灭
                if (checkAllFlag) {
                    $("thead input[name=resCheckBox]").prop('checked', false);
                    $("thead input[name=resCheckBox]").next().removeClass("layui-form-checked")
                    checkAllFlag = false
                }
            }
        }

    });


</script>
</html>
